﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using log4net;


namespace DevMaker.SIC.Dao {
    public static class GenericQueryUtil {

        private static readonly ILog Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        public static IDataReader ExecuteGenericquery(string query) {
            try {
                var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SIC_Connection"].ToString());
                var objReader = SqlHelper.ExecuteDataset(conn, CommandType.Text, query);
                return objReader.CreateDataReader();
            } catch (Exception e) {
                Log.Error(e);
            }
            return null;
        }

        public static DataSet ExecuteGenericqueryDataset(string query) {
            try {
                var dataset = new DataSet();
                var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SIC_Connection"].ToString());
                var adapter = new SqlDataAdapter(query, conn);
                adapter.Fill(dataset);
                return dataset;
            } catch (Exception e) {
                Log.Error(e);
            }
            return null;
        }

        public static DataSet ExecuteGenericQueryDataSet(SqlCommand command) {

            var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SIC_Connection"].ToString());
            SqlDataAdapter da = new SqlDataAdapter { SelectCommand = command };
            DataSet ds = new DataSet();
            command.Connection = conn;
            da.Fill(ds);
            command.Connection.Close();

            return ds;
        }
    }
}
